package org.hismig.test.migration;

import java.util.List;

import junit.framework.TestCase;

import org.hismig.cfg.Configuration;
import org.hismig.migration.Constructor;
import org.hismig.migration.executor.DBExecutor;
import org.hismig.migration.executor.TableExecutor;
import org.hismig.migration.strategy.IStrategy;
import org.hismig.migration.strategy.RSMigration;
import org.hismig.scheduler.ITrigger;
import org.hismig.scheduler.QuartzTrigger;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-07-04 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class ConstructorTester extends TestCase {

	private Constructor constructor;

	@Override
	protected void setUp() throws Exception {
		constructor = new Constructor();
		Configuration configuration = new Configuration(ConstructorTester.class
				.getResource("HisMig.cfg.xml").toURI());

		constructor.init(configuration);
	}

	public void testTrigger() {
		ITrigger trigger = constructor.getTrigger();

		assertNotNull(trigger);

		assertTrue(trigger instanceof QuartzTrigger);
	}

	public void testStrategy() {
		IStrategy strategy = constructor.getStrategy();

		assertNotNull(strategy);

		assertTrue(strategy instanceof RSMigration);
	}

	public void testDBExecutor() {
		List<DBExecutor> dbExecutors = constructor.getDbExecutors();

		assertNotNull(dbExecutors);

		assertFalse(dbExecutors.isEmpty());

		DBExecutor dbExecutor = dbExecutors.get(0);

		assertNotNull(dbExecutor.getDataBase());
		assertNotNull(dbExecutor.getDistAfterExtExecutor());
		assertNotNull(dbExecutor.getDistDataSource());
		assertNotNull(dbExecutor.getDistPreExtExecutor());
		assertNotNull(dbExecutor.getDynamicPartition());
		assertNotNull(dbExecutor.getParamBuilders());
		assertFalse(dbExecutor.getParamBuilders().isEmpty());
		assertNotNull(dbExecutor.getSourceAfterExtExecutor());
		assertNotNull(dbExecutor.getSourceDataSource());
		assertNotNull(dbExecutor.getSourcePreExtExecutor());
		assertNotNull(dbExecutor.getTableExecutors());
		assertFalse(dbExecutor.getTableExecutors().isEmpty());

		DBExecutor dbExecutor2 = dbExecutors.get(1);

		assertNotNull(dbExecutor2.getDataBase());
		assertNull(dbExecutor2.getDistAfterExtExecutor());
		assertNotNull(dbExecutor2.getDistDataSource());
		assertNull(dbExecutor2.getDistPreExtExecutor());
		assertNull(dbExecutor2.getDynamicPartition());
		assertNotNull(dbExecutor2.getParamBuilders());
		assertFalse(dbExecutor2.getParamBuilders().isEmpty());
		assertNull(dbExecutor2.getSourceAfterExtExecutor());
		assertNotNull(dbExecutor2.getSourceDataSource());
		assertNull(dbExecutor2.getSourcePreExtExecutor());
		assertNotNull(dbExecutor2.getTableExecutors());
		assertFalse(dbExecutor2.getTableExecutors().isEmpty());
	}

	public void testTableExecutor() {

		List<TableExecutor> tableExecutors = constructor.getDbExecutors()
				.get(0).getTableExecutors();

		TableExecutor tableExecutor = tableExecutors.get(0);

		assertNotNull(tableExecutor.getDbExecutorBase());
		assertNotNull(tableExecutor.getDistAfterExtExecutor());
		assertNotNull(tableExecutor.getDistPreExtExecutor());
		assertNotNull(tableExecutor.getParamBuilders());
		assertFalse(tableExecutor.getParamBuilders().isEmpty());
		assertNotNull(tableExecutor.getSourceAfterExtExecutor());
		assertNotNull(tableExecutor.getSourcePreExtExecutor());
		assertNotNull(tableExecutor.getStrategy());
		assertNotNull(tableExecutor.getTable());

		TableExecutor tableExecutor2 = tableExecutors.get(1);

		assertNotNull(tableExecutor2.getDbExecutorBase());
		assertNotNull(tableExecutor2.getDistAfterExtExecutor());
		assertNotNull(tableExecutor2.getDistPreExtExecutor());
		assertNotNull(tableExecutor2.getParamBuilders());
		assertFalse(tableExecutor2.getParamBuilders().isEmpty());
		assertNotNull(tableExecutor2.getSourceAfterExtExecutor());
		assertNotNull(tableExecutor2.getSourcePreExtExecutor());
		assertNotNull(tableExecutor2.getStrategy());
		assertNotNull(tableExecutor2.getTable());

		List<TableExecutor> tableExecutors2 = constructor.getDbExecutors()
				.get(1).getTableExecutors();

		TableExecutor tableExecutor3 = tableExecutors2.get(0);

		assertNotNull(tableExecutor3.getDbExecutorBase());
		assertNull(tableExecutor3.getDistAfterExtExecutor());
		assertNull(tableExecutor3.getDistPreExtExecutor());
		assertNotNull(tableExecutor3.getParamBuilders());
		assertTrue(tableExecutor3.getParamBuilders().isEmpty());
		assertNull(tableExecutor3.getSourceAfterExtExecutor());
		assertNull(tableExecutor3.getSourcePreExtExecutor());
		assertNotNull(tableExecutor3.getStrategy());
		assertNotNull(tableExecutor3.getTable());

		TableExecutor tableExecutor4 = tableExecutors2.get(1);

		assertNotNull(tableExecutor4.getDbExecutorBase());
		assertNull(tableExecutor4.getDistAfterExtExecutor());
		assertNull(tableExecutor4.getDistPreExtExecutor());
		assertNotNull(tableExecutor4.getParamBuilders());
		assertTrue(tableExecutor4.getParamBuilders().isEmpty());
		assertNull(tableExecutor4.getSourceAfterExtExecutor());
		assertNull(tableExecutor4.getSourcePreExtExecutor());
		assertNotNull(tableExecutor4.getStrategy());
		assertNotNull(tableExecutor4.getTable());

	}

}
